import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import { viteMockServe } from 'vite-plugin-mock'
import PkgConfig from 'vite-plugin-package-config'
import OptimizationPersist from 'vite-plugin-optimize-persist'
import vitePluginCompression from 'vite-plugin-compression'
import vitePluginImp from 'vite-plugin-imp'
import reactRefresh from '@vitejs/plugin-react-refresh'

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  return {
    plugins: [
      react(),
      reactRefresh(), // 支持自动刷新
      vitePluginImp({ // 自动导入组件库样式
        libList: [
          {
            libName: 'antd',
            style: name => `antd/lib/${name}/style/index.less`,
          }
        ]
      }),
      viteMockServe({
        // default
        mockPath: './mock',
        localEnabled: command === 'serve',
        supportTs: false
      }),
      vitePluginCompression(), // gzip 压缩
      // 优化启动速度，解决开发环境加载缓慢问题
      PkgConfig({ packageJsonPath: './vite/vite.json' }),
      OptimizationPersist(),
    ],

    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },

    css: {
      preprocessorOptions: {
        less: { 
          // 支持 less 内联 JavaScript 
          javascriptEnabled: true
        }
      }
    }
  }
}
)
